package com.luwanglin.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luwanglin.commonutils.R;
import com.luwanglin.eduservice.entity.EduCourse;
import com.luwanglin.eduservice.entity.EduTeacher;
import com.luwanglin.eduservice.entity.vo.TeacherQuery;
import com.luwanglin.eduservice.service.EduCourseService;
import com.luwanglin.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-02
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
//@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private EduCourseService courseService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher() {
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }


    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true)
                           @PathVariable("id") String id) {
        boolean b = teacherService.deleteTeacherById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error().message("delete failed");
        }

    }

    @GetMapping("pageTeacher/{curPage}/{limit}")
    public R pageListTeacher(
            @ApiParam(value = "当前页码", required = true)
            @PathVariable("curPage") int curPage,
            @PathVariable("limit") int limit) {
        Page<EduTeacher> page = new Page<>(curPage, limit);
        teacherService.page(page, null);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * order by
     */
    @ApiOperation(value = "分页讲师列表")
    @PostMapping("{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "teacherQuery", value = "查询对象")
            @RequestBody(required = false) TeacherQuery teacherQuery) {
        System.out.println(page + "," + limit);
        Page<EduTeacher> pageParam = new Page<>(page, limit);
        teacherService.pageQuery(pageParam, teacherQuery);

       /* try {
            int a = 1 / 0;
        } catch (Exception e) {
            throw new GuliException(20001, "guli exception");
        }*/
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher) {

        boolean save = teacherService.save(teacher);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("{id}")
    public R getTeacher(@PathVariable("id") String id) {
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("item", teacher);
    }

    /*@PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = teacherService.updateById(eduTeacher);
        return flag ? R.ok() : R.error();
    }*/

    @PutMapping("{id}")
    public R updateTeacher(@PathVariable("id") String id, @RequestBody EduTeacher teacher) {
        teacher.setId(id);
        boolean flag = teacherService.updateById(teacher);
        return flag ? R.ok() : R.error();
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping(value = "front/{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {

        //查询讲师信息
        EduTeacher teacher = teacherService.getById(id);
        //根据讲师id查询这个讲师的课程列表
        List<EduCourse> courseList = courseService.selectByTeacherId(id);
        return R.ok().data("teacher", teacher).data("courseList", courseList);
    }
}

